<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>物业管理系统</title>

<base href="<%=basePath%>">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/vivo.css">
<link rel="stylesheet" href="css/cmxform.css">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="js/grid/bsgrid.all.min.css" />
<link rel="stylesheet" type="text/css"
	href="js/Hui-iconfont/1.0.7/iconfont.css" />
<link rel="stylesheet" type="text/css" href="js/icheck/icheck.css" />


<script src="js/jquery-1.11.1.js"></script>
<link href="js/select2/select2.min.css" type="text/css" rel="stylesheet" />
<script src="js/select2/select2.min.js"></script>
<script src="js/jquery.validation/1.14.0/jquery.validate.min.js"></script>
<script src="js/jquery.validation/1.14.0/messages_zh.min.js"></script>
<script type="text/javascript" src="js/sharefunction.js"></script>
<script language="javascript" type="text/javascript"
	src="<%=basePath%>js/My97DatePicker/WdatePicker.js"></script>

<style type="text/css">
.primary-vdd {
	color: #fff;
	background-color: #5a98de;
	border-color: #5a98de
}

.primary-vdd:hover {
	color: #fff;
	background-color: #0a6999;
	border-color: #0a6999
}
</style>
<script type="text/javascript">

	//初始化赋值
	function initDeptMsg() {
		var deptId = $("#commentForm").find("#id").val();
		if (deptId == "") {
			//重置表单
			$("#commentForm")[0].reset();
		} else {
			jQuery.ajax({
				type : "post",
				async : false,
				url : "admin/org/dept/getDeptById",
				dataType : "json",
				data : {
					id : deptId
				},
				success : function(data) {
					if (data != null) {
						$("#parentId").val(data.parentId).trigger("change");
						$("#commentForm #sortNum").val(data.sortNum);
						$("#commentForm #orgName").val(data.orgName);
						$("#commentForm #orgCode").val(data.orgCode);
						$("#commentForm #orgType").val(data.orgType).trigger("change");
						$("#commentForm #orgProperty").val(data.orgProperty).trigger("change");

					}
				}
			});

		}
	}

	//绑定字典内容到指定的Select控件
	function BindSelect(ctrlName, url) {
		var control = $('#' + ctrlName);
		//设置Select2的处理
		control.select2({
			allowClear : true,
			escapeMarkup : function(m) {
				return m;
			}
		});

		//绑定Ajax的内容
		jQuery.ajax({
			type : "get",
			async : false,
			url : url,
			dataType : "json",
			success : function(data) {
			//control.empty();//清空下拉框
			var dataArray = eval('(' + data + ')');
			control.append("<option value='root'>&nbsp;根目录</option>");
			for (var i = 0; i < dataArray.length; i++) {
				control.append("<option value='" + dataArray[i].id + "'>&nbsp;" + dataArray[i].orgName + "</option>");
			}
			//绑定默认值
			control.val("root").trigger("change");
			}
		});

	}

	$.validator.setDefaults({
		submitHandler : function() {
			saveDocument();
		}
	});

	$(document).ready(function() {
		var url = "admin/org/dept/json/root";
		BindSelect("parentId", url);

		$("#orgType").select2({
			minimumResultsForSearch : -1
		});
		$("#orgProperty").select2({
			minimumResultsForSearch : -1
		});

		$("#commentForm").validate({
			errorPlacement : function(error, element) {
				// Append error within linked label
				$(element).next().append(error);
			},
			errorElement : "span",
			rules : {
				reading : {
					maxlength : 9,
					max : 999999,
					min : 0
				}
			},
			messages : {
				typeName : {
					required : " 必需字段!",
					minlength : " (不能少于 3 个字母)"
				}
			}
		});
		initDeptMsg();
	});

	function saveDocument() {
		var id = $("#id").val();
		var parentId = $("#parentId").val();
		var sortNum = $("#sortNum").val();
		if(sortNum==''||sortNum==undefined){
			sortNum=null;
		}
		var orgName = $("#orgName").val();
		var orgCode = $("#orgCode").val();
		var orgType = $("#orgType").val();
		var orgProperty = $("#orgProperty").val();
		//layer.confirm("确定保存吗？",{icon:3},function(i){
		var url = "admin/org/dept/save";
		jQuery.ajax({
			type : "post",
			async : false,
			url : url,
			dataType : "json",
			data : {
				id : id,
				parentId : parentId,
				sortNum : sortNum,
				orgName : orgName,
				orgCode : orgCode,
				orgType : orgType,
				orgProperty : orgProperty
			},
			success : function(data) {

				if (data.success) {
					alert(data.msg);
					location.href="/pms/admin/org/dept/list";
				} else {
					layer.alert(data.msg, {
						icon : 7
					});
				}
			}
		});
	}
</script>

</head>
<body>
	<form class="cmxform" id="commentForm" method="post">
		<div style="padding: 20px">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h5 class="panel-title">部门信息</h5>
				</div>
				<div class="panel-body">
					<table cellpadding="10" cellspacing="5" class="mytable">
						<tr>
							<td width="90">上级组织</td>
							<td width="300px"><select id="parentId"
								style="width: 260px;">
							</select></td>
							<td width="90">组织排序</td>
							<td><input type="text" name="sortNum" id="sortNum">
							</td>
						</tr>
						<tr>
							<td width="90">组织名称</td>
							<td><input type="text" name="orgName" id="orgName" required />
								<font color="red"> *</font> <span style="color: red"></span></td>
							<td width="90">组织编号</td>
							<td><input type="text" name="orgCode" id="orgCode">
								<span style="color: red"></span></td>
						</tr>
						<tr>
							<td width="90">组织类型</td>
							<td><select id="orgType" style="width: 90px;">
									<option value="0">部门</option>
									<option value="1">公司</option>
							</select></td>
							<td width="90">组织性质</td>
							<td><select id="orgProperty" style="width: 90px;">
									<option value="0">职能组织</option>
									<option value="1">项目组织</option>
							</select></td>
						</tr>
					</table>
				</div>
			</div>
			<div style="width: 100%; text-align: center; padding-top: 20px">
				<button class="btn btn-primary radius primary-vdd" type="submit">保
					存</button>
				<!-- <a class="btn btn-default radius" onClick="closeLayer();"> 取 消 </a>
				<br /> <br /> -->
			</div>
		</div>

		<div style="display:none">
			<input id="id" name="id" type="hidden" value="${id}" />
		</div>
	</form>
</body>
</html>

